<?php

namespace App\Http\Controllers\Admin;

use App\Captcha as AppCaptcha;
use App\CaptchaData;
use App\Http\Controllers\Api\Captcha;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\DB;
/**
 * 后端的接口，获取此控制器中的接口，每次都需要进行权限验证
 */
class Api extends Controller
{
    //获取管理员主界面数据
    function getindex_data(Request $req){
        if(!Power::validate($req)){
            //验证未通过
            return redirect('admin/login');
        }

        //获取今日登录数量
        $login_count = DB::table('log')->where('log_number',date('Ymd'))->where('message','登录')->first();
        !$login_count && $login_count = ['count'=>0];
        //获取今日注册数量
        $register_count = DB::table('log')->where('log_number',date('Ymd'))->where('message','注册')->first();
        !$register_count && $register_count = ['count'=>0];
        //获取配置项
        $login = DB::table('config')->where('mark','login')->first();
        $register = DB::table('config')->where('mark','register')->first();
        return ['code'=>200,'message'=>'获取成功','data'=>[
            'login'=>$login_count,
            'register'=>$register_count?$register_count:[],
            'login_config'=>$login,
            'register_config'=>$register,
            ]
        ];
    }

    //设置配置选项
    function setConfig(Request $req){
        //更改的类型
        $config_id = $req->configId;
        //状态值
        $status = $req->status;
        DB::table('config')->where('id',$config_id)->update(['status'=>$status]);
        return ['code'=>200,'message'=>'更改成功'];
    }

    //获取今天的日志
    function getLog(Request $req){
        $mark = $req->mark;
        $log = DB::table('log')->where('log_number',date('Ymd'))->where('message',$mark)->first();

        if(!$log){
            return ['code'=>200,'data'=>[],'message'=>'获取为空'];
        }
        $log = DB::table('log_details')->where('log_id',$log->id)->get();
        foreach($log as $a){
            $a->user_account = DB::table('user')->where('id',$a->user_id)->value('account');
        }
        return ['code'=>200,'data'=>$log,'message'=>'获取成功'];
    }

    //获取用户列表
    function getUser_list(Request $req){
        $user = DB::table('user')->get(['id','account','email','login_time','create_time']);
        //获取用户站点数量
        foreach($user as $a){
            $a->count =\count(DB::table('captcha')->where('user_id',$a->id)->get());
        }   
        return ['code'=>200,'message'=>'获取成功','data'=>$user];
    }

    //获取用户数据
    function getUser_data(Request $req){
        $user_id = $req->userId;
        $user = DB::table('user')->where('id',$user_id)->first(['id','account','email','login_time','create_time']);
        $user->captcha = DB::table('captcha')->where('user_id',$user->id)->get();
        foreach($user->captcha as $a){
            $a->option = DB::table('captcha_data')->where('captcha_id',$a->id)->get();
        }
        return ['code'=>200,'message'=>'获取成功','data'=>$user];
    }

    //更新安全码
    public function updateM(Request $req){
        //修改选项
        $captcha = $req->captcha;
        $option = $captcha['option'];
        
        //删除option
        unset($captcha['option']);
        //删除的选项
        $delect = $req->delect;
        $user_id = $req->user_id;

        //执行删除
        foreach($delect as $a){
            DB::table('captcha_data')->where('id',$a)->delete();
        }
        //执行修改主体,如果获取失败则创建一条记录
        if(empty($captcha['id'])){
            //添加主体新数据返回id
            $id = DB::table('captcha')->insertGetId($captcha);
        }else{
            //修改
            $id = $captcha['id'];
            // AppCaptcha 是 Captcha的别名
            $a = AppCaptcha::where('id',$id)->first();
            $a->code = $captcha['code'];
            $a->title = $captcha['title'];
            $a->save();
        }

        /***
         *   captcha_id: 1
         *   id: 1
         *   port: "80"
         *   url: "http://www.baidu.com"
         */
        //添加选项或修改选项
        foreach($option as $a){
            if($a['captcha_id']){
                //有id直接修改
                CaptchaData::where('id',$a['id'])->update($a);
            }else{
                //没有id说明是新数据添加
                $a['captcha_id'] = $id;
                if(!DB::table('captcha_data')->insertGetId($a)){
                    return ['code'=>-2,'message'=>'选项添加失败','data'=>$a];
                }
            }
        }
        $data=null;
        if(empty($captcha['id'])){
            $data = DB::table('captcha')->where('id',$id)->first();
            $data->option = DB::table('captcha_data')->where('captcha_id',$id)->get();
        }
        return ['code'=>200,'message'=>'添加/修改成功','data'=>$data];
    }

    //删除安全码
    public function delectM(Request $req){
        $captcha_id = $req->captcha_id;
        if($captcha_id){
            AppCaptcha::where('id',$captcha_id)->delete();
            CaptchaData::where('captcha_id',$captcha_id)->delete();
            return ['code'=>200,'message'=>'删除成功'];
        }else{
            return ['code'=>-1,'message'=>'参数错误'];
        }
    }


}
